import React,{Component} from 'react';
import { Redirect, Route ,Switch} from 'react-router-dom';
import { connect } from 'react-redux';
import { Layout } from 'antd';
import Header from './header/header';
import LeftNav from './left_nav/leftNav';
import Home from '../../components/home/home';
import Error from '../../components/error/error';

import Attribute from '../course/attribute/attribute';
import CContent from '../course/content/content';
import Relation from '../course/relation/relation';
import Teacher from '../teacher/teacher';
import Student from '../student/student';
import Article from '../push/article/article';
import Notice from '../push/notice/notice';
import TeacherDetail from '../teacher/detail';
import TeacherUpdate from '../teacher/add_update';
import ContentDetail from '../course/content/detail';
import ContentUpdate from '../course/content/add_update';
import NoticeUpdate from '../push/notice/add_update';
import ArticleUpdate from '../push/article/add_update';
import Curriculum from '../teacher-center/curriculum';
import CurriculumDetail from '../teacher-center/detail';
import Subject from '../teacher-center/Subject';
import Score from '../teacher-center/Score';
import TeacherInfoUpdate from '../teacher-center/TeacherUpdate';
import TeacherInfo from '../teacher-center/teacherInfo';
import Password from '../safety/Password';
import AllNotice from '../teacher-center/allNotice';
import FeedBack from '../feedback/feedback';

import './admin.less';
const {  Footer, Sider, Content } = Layout;
@connect(
    state=>({
    // state.userInfo中的userInfo与/reducer/index.js中的对象名一致
        userInfo:state.userInfo,
    }), 
    {
        
    }
)
class Admin extends Component {
    state={
        collapsed: false,
    }
    onCollapse = collapsed => {
        console.log(collapsed);
        this.setState({ collapsed });
      };
    // 在render里，若想实现跳转，最好用<Redirect/>
    getToRouter = () =>{
        const { collapsed } = this.state;
        return (
            <Layout className="admin">
                <Sider className="side"  collapsible collapsed={collapsed} onCollapse={this.onCollapse}>
                    <LeftNav />
                </Sider>
                <Layout>
                    <Header className="header">Header</Header>
                    <Content className="content">
                        <Switch>
                            <Route path='/training/home' component={Home}/>
                            {/* 管理员 */}
                            <Route path='/training/admin/course/attribute' exact component={Attribute}/>
                            <Route path='/training/admin/course/content' exact component={CContent}/>
                            <Route path='/training/admin/course/content/detail/:id' component={ContentDetail}/>
                            <Route path='/training/admin/course/relation' component={Relation}/>
                            <Route path='/training/admin/teacher' exact component={Teacher}/>
                            <Route path='/training/admin/teacher/detail/:id' component={TeacherDetail}/>
                            <Route path='/training/admin/student' exact component={Student}/>
                            <Route path='/training/admin/push/article' exact component={Article}/>
                            <Route path='/training/admin/push/article/update' exact component={ArticleUpdate}/>
                            <Route path='/training/admin/push/article/update/:id' component={ArticleUpdate}/>
                            <Route path='/training/admin/push/notice' exact component={Notice}/>
                            <Route path='/training/admin/push/notice/update' exact component={NoticeUpdate}/>
                            <Route path='/training/admin/push/notice/update/:id' component={NoticeUpdate}/>
                            <Route path='/training/admin/teacher/update'  exact component={TeacherUpdate}/>
                            <Route path='/training/admin/teacher/update/:id' component={TeacherUpdate} />
                            <Route path='/training/admin/course/content/update'  exact component={ContentUpdate}/>
                            <Route path='/training/admin/course/content/update/:id' component={ContentUpdate}/>
                            <Route path='/training/admin/safety/password' component={Password}/>
                            <Route path='/training/admin/feedback' component={FeedBack}/>
                            {/* 讲师 */}
                            <Route path='/training/teacher/curriculum' exact component={Curriculum}/>
                            <Route path='/training/teacher/curriculum/detail/:id' component={CurriculumDetail}/>
                            <Route path='/training/teacher/personal/subject' exact component={Subject}/>
                            <Route path='/training/teacher/personal/subject/detail/:id' component={CurriculumDetail}/>
                            <Route path='/training/teacher/personal/subject/score/:id' component={Score}/>
                            <Route path='/training/teacher/personal/info' exact component={TeacherInfo}/>
                            <Route path='/training/teacher/personal/info/update' component={TeacherInfoUpdate}/>
                            <Route path='/training/teacher/safety/password' component={Password}/>
                            <Route path='/training/teacher/notice/list' component={AllNotice}/>

                            <Route path='/admin/error' component={Error}/>
                            <Redirect to="/training/home"/>
                        </Switch>
                    </Content>
                    <Footer className="footer">
                        推荐使用谷歌浏览器，获取最佳用户体验
                    </Footer>
                </Layout>
            </Layout>
        );
    }
    render(){
        const { isLogin,user } = this.props.userInfo;
        if(!isLogin){
             return <Redirect to='/login'/>
        }else{
            // const {pathname} = this.props.location
            // let pathKey = pathname.split('/').reverse()[0]
            // let pathKey = pathname.split('/').slice(1)
            // console.log(user.role.menus.indexOf(pathKey) !== -1);
            // if(user.username === 'admin' || user.role.menus.indexOf(pathKey) !== -1){ 
             if(user.username){ 
                return this.getToRouter()
            }
            else{
                return <Redirect to='/login'/>
            }

        }
    }
}
export default Admin;
/* export default connect(
    state=>({
        // state.userInfo中的userInfo与/reducer/index.js中的对象名一致
        userInfo:state.userInfo
    }), 
    {
        deleteUserInfo:createDeleteUserInfoAction
    }
)(Admin); */